﻿var CompanyHistoryGrid = CompanyHistoryGrid || {};
CompanyHistoryGrid = function () {
    var gridInEditMode = false;
    var scratchDropDownChanged = false;
    var CompanyHistoryDataSource = new kendo.data.DataSource({
        transport: {
            read: {
                url: "/CompanyHistory/GetCompanyHistory",
                type: "get",
                dataType: "json"
            },
            parameterMap: function (options) {
                return kendo.stringify(options); // kendo.stringify serializes to JSON string
            }

        },

        pageSize: 5
    
    });
  
    return {
        CompanyHistoryDataSource: CompanyHistoryDataSource,
        gridInEditMode: gridInEditMode,
        scratchDropDownChanged: scratchDropDownChanged
    }
}();




$(function () {

    $(window).on("beforeunload", function () {

        if (CompanyHistoryGrid.gridInEditMode) {

            return "Are you sure you want to discard all your changes."

        }

    });
    $("#CompanyHistoryGrid").kendoGrid({
        dataSource: CompanyHistoryGrid.CompanyHistoryDataSource,
        scrollable: true,
        height: "250px",
        toolbar: ["create"], //, "save", "cancel"],
        pageable: true,
        height: 300,
        columns: [
     // { field: "LogsDate", title: "LogsDate*", filterable: false },
      { field: "name", title: "Name", filterable: false, width: "8em" },
      { field: "phone", title: "Phone", width: "8em" },
        { field: "fax", title: "Fax", width: "8em" },
          { field: "clientType", title: "ClientType", width: "8em" },
            { field: "companyType", title: "CompanyType", width: "8em" },
       {
           field: "relationStart", title: "Start Date", hidden: true, filterable: false, format: "{0:MM/dd/yyyy HH:mm tt}",
                    editor: dateTimeEditor,
              filterable: {
                    ui: "datetimepicker"
               }
                  },

             {
                 field: "relationEnd", title: "End Date", hidden: true, editable: true, filterable: false, format: "{0:MM/dd/yyyy HH:mm tt}",
                 editor: dateTimeEditor,
              filterable: {
                       ui: "datetimepicker"
                     }
                             },

             {
             field: "updateDate", title: "Update Date", hidden: true, editable: true, filterable: false, format: "{0:MM/dd/yyyy HH:mm tt}",
           editor: dateTimeEditor, width: "8em",
                filterable: {
                   ui: "datetimepicker"
                                 }
                             },
     
      {
          title: "&nbsp;",
          width: "25em",
          //command: ["edit", "destroy"]
          command: [

              "edit",
              {
                  name: "Delete",


                  click: function (e) {
                     
                      var tr = $(e.target).closest("tr"); //get the row for deletion
                      var dataItem = this.dataItem(tr); //get the row data so it can be referred later
                      //see what is coming in dataItem
                      //if (dataItem > 0) {
                      //    $("#delValidation").addClass('in')
                      //}
                      //else {

                          deleteTopicConfirmation(e, this);
                      ////}
                      return false;
                  }
              }
          ]
      }
        ],
        filterable: {
            extra: false,
            operators: {
                string: {
                    eq: "Is equal to",
                    neq: "Is not equal to"
                }
            }
        },

        editable: "popup",
        sortable: {
            mode: "single",
            allowUnsort: false
        },
        selectable: true,
        // change: onTopicChange,
        // dataBound: onTopicsGridDataBound,
      
        cancel: function () {
            JobGrid.gridInEditMode = false;
        }
    });

    CompanyHistoryGrid.jobDataSource.bind("requestEnd", showSuccessMessage);


    function showSuccessMessage(e) {
        if (e.type === "update" || e.type === "create") {
            JobGrid.gridInEditMode = false;
            $("#saveConfirm").addClass('in');
            e.sender.read();
        }
    }


    $(".close").click(function () {
        $(this).parent().removeClass('in');
    });


    function deleteTopicConfirmation(e, widget) {
        var grid = $('#CompanyHistoryGrid').data("kendoGrid");
        deleteConfirmation(e, widget, grid);
    }

    function deleteConfirmation(e, widget, grid) {

        var currentRow = $(e.target).closest("tr"); //get the row for deletion

        var currentRowData = widget.dataItem(currentRow); //get the row data so it can be  referred later

        var kendoWindow = $("<div />").kendoWindow({

            title: "Confirmation",
            resizable: true,
            modal: true,
            width: "400px"
        });

        kendoWindow.data("kendoWindow")

            .content($("#delete-confirmation").html())

            .center().open();

        kendoWindow

            .find(".delete-confirm,.delete-cancel")
            .click(function () {
                if ($(this).hasClass("delete-confirm")) {

                    grid.dataSource.remove(currentRowData);

                    grid.dataSource.sync();

                }

                $(this).closest("[data-role=window]").kendoWindow("close");

            });

    }

});